package ywh.player
{
	import flash.display.DisplayObject;
	
	/**
	 * 抽象Player
	 * @author WilliWHYang
	 */
	public class AbstractPlayer extends AbstractShower
	{
		protected var _targetTime:Number;
		protected var _targetTimeOffset:Number;
		protected var _needSeek:Boolean = false;
		
		public function AbstractPlayer()
		{
		}
		
		protected function get offset():Number
		{
			return 1;
		}
		
		override public function seekTo(value:Number):void
		{
			if (isNaN(_time) || value >= 0 && value < duration && Math.abs(value - _time) > offset)
			{
				_needSeek = true;
				_targetTime = value;
				_targetTimeOffset = value + offset;
			}
		}
		
		override public function play():void
		{
			_pPlay();
		}
		
		override public function pause():void
		{
			_pPause();
		}
		
		/**
		 * 播放器特异性方法，需要在具体的播放器中被覆盖;
		 * 1.需要在这个方法中给所有需要获取的动态改变的属性赋值;
		 * 如：状态_status,当前时间_time，用以获取;
		 */
		override public function preSyncHandle():void
		{
			//===========未播放的时候==============;
			if (!state || state == PlayerState.EMPTY)
			{
				_time = NaN;
				return;
			}
			
			var tempTime:Number = _pTime;
			var tempstate:String = _pState;
			if (tempstate == PlayerState.PLAYBACK_ERROR)
			{
				trace("[AbstractPlayer:preSyncHandle]发生错误了！");
				return;
			}
			//========需要跳转================================	
			if (_needSeek)
			{
				//此处为需要跳转到指定时间的处理方式：
				_time = _targetTime;
				state = PlayerState.SEEKING;
				if (canSeekTo(_targetTime))
				{
					//此处为需要跳转且可以跳转
					_pSeekTo(_targetTime);
					_needSeek = false;
					return;
				}
				else
				{
					//此处为需要跳转但不可以跳转					
					return;
				}
			}
			else if (!isNaN(_targetTime))
			{
				//已跳转后查询时间区段的处理方式;
				if (tempTime >= _targetTime && tempTime < _targetTimeOffset)
				{
					//进入到区段内的处理方式;
					_targetTime = NaN;
					_targetTimeOffset = NaN;
					
					_time = tempTime;
					state = tempstate
					return;
				}
				else
				{
					_time = _targetTime;
					//区段外的处理方式;
					state = PlayerState.SEEKING;
					return;
				}
			}
			//=============特殊状态===================
			if (isNaN(tempTime) || tempTime <= 0)
			{
				// 当前时间不可见;
				_time = _time || 0;
				state = tempstate;
				return;
			}
			if (tempTime >= duration)
			{
				//当前时间超过播放时间;
				_time = duration;
				state = PlayerState.ENDED;
				_pPause();
				return;
			}
			//========正常播放状态==========================
			
			if (tempTime == _time)
			{
				//如果播放器时间为之前时间，则强制为暂停状态;
				state = PlayerState.PAUSED;
				return;
			}
			else
			{
				//正常播放状态;
				_time = tempTime;
				state = PlayerState.PLAYING;
				return;
			}
		
		}
		
		override protected function canSeekTo(value:Number):Boolean
		{
			if (value >= duration)
			{
				return false;
			}
			return true;
		}
		
		override public function get duration():Number
		{
			if (isNaN(_duration))
			{
				if (isNaN(_pDuration))
				{
					return NaN;
				}
				_duration = Math.min(_playItem.duration, _pDuration);
			}
			return _duration;
		}
		
		//=====================以下为播放器特异性方法需要覆盖========================================
		
		/**
		 * [播放器特异性方法需要覆盖] 播放器播放;
		 */
		protected function _pPlay():void
		{
		
		}
		
		/**
		 * [播放器特异性方法需要覆盖] 播放器暂停;
		 */
		protected function _pPause():void
		{
		
		}
		
		/**
		 *  [播放器特异性方法需要覆盖] 根据_targetTime，进行设置播放器时间;
		 */
		protected function _pSeekTo(value:Number):void
		{
		}
		
		/**
		 *  [播放器特异性方法需要覆盖] 获取播放器时间;
		 */
		protected function get _pTime():Number
		{
			return NaN;
		}
		
		/**
		 *  [播放器特异性方法需要覆盖] 获取媒体真实长度;
		 */
		protected function get _pDuration():Number
		{
			return NaN;
		}
	}
}